# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""
 -------------------------------------------------
    File Name:     request_handler.py
    Description:   请求引擎 http.client 与 Requests
 -------------------------------------------------
 """

import allure
import requests

from apitesting.common.util.data_formatter import json_format
from apitesting.common.util.logger import log


class ApiEngin:

    @staticmethod
    @allure.step("发送请求参数")
    def request(
            url: str,
            method: str,
            headers: dict = None,
            data=None,
            json_data=None,
            files=None,
            **kwargs):
        """
        requests 引擎

        :requests: 接受多个关键字，这里没有具体去一个一个拿
        :return:
        """
        # log.info(f"常规请求参数")
        # log.info(f"method: {method}")
        # log.info(f"url: {url}")
        # log.info(f"headers: {headers}")
        # log.info(f"data: {data}")
        # log.info(f"json: {json_data}")

        log.info(f"Sending {method} request to {url}")

        try:
            # log.debug(f"其他请求参数:\n{json_format(data=kwargs)}")
            response = requests.Session().request(
                url=url,
                method=method,
                headers=headers,
                data=data,
                json=json_data,
                files=files,
                **kwargs
            )

            response.raise_for_status()

            log.info(f"响应结果:\n{json_format(data=response.json())}")

        except Exception as e:
            log.error(f"发送请求异常，请检查!!!")
            raise Exception(f"发送请求异常!!!")
        except requests.exceptions.RequestException as e:
            log.error(f"发送请求时捕获可能出现的异常，请检查!!!")
            raise

        return response

    @staticmethod
    def api_http_client():
        """
        http_client 引擎

        :return:
        """
        log.debug("===================开始使用 http_client 引擎发送请求...============================")
        pass
